package cz.muni.fi.pa165.ddtroops.web.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.pa165.ddtroops.dto.RaceDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.RaceService;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

/**
 *
 * @author Svoboda
 */
public class RaceRest extends HttpServlet {
    
    @Autowired
    @Qualifier("raceWebService")
    private RaceService service;
    private ObjectMapper mapper = new ObjectMapper();
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        request.setCharacterEncoding("utf-8");
        String path = request.getPathInfo();
        response.setContentType("application/json");
        
        if (RestHelpers.isAll(path)) {
            mapper.writeValue(response.getOutputStream(), service.getAll());
        } else if (RestHelpers.isNonnegative(RestHelpers.getFirst(path))) {
            mapper.writeValue(response.getOutputStream(), service.getById((long) Integer.parseInt(RestHelpers.getFirst(path))));
        } else {
            RestHelpers.printErrorMessage(response);
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        request.setCharacterEncoding("utf-8");
        
        String path = request.getPathInfo();
        
        if (RestHelpers.isRoot(path)) {
            
            try {
                RaceDTO r = mapper.readValue(request.getInputStream(), RaceDTO.class);
                service.create(r);
                response.setHeader("Location", path);
            } catch (IllegalArgumentException | NullPointerException e) {
                RestHelpers.printErrorMessage(response);
            }
        } else {
            RestHelpers.printErrorMessage(response);
        }
    }
    
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        request.setCharacterEncoding("utf-8");
        String path = request.getPathInfo();
        
        if (RestHelpers.isNonnegative(RestHelpers.getFirst(path))) {
            try {
                service.delete(service.getById((long) Integer.parseInt(RestHelpers.getFirst(path))));
            } catch (NullPointerException e) {
                RestHelpers.printErrorMessage(response);
            }
        } else {
            RestHelpers.printErrorMessage(response);
        }
    }
    
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        request.setCharacterEncoding("utf-8");
        String path = request.getPathInfo();
        
        if (RestHelpers.isNonnegative(RestHelpers.getFirst(path))) {
            try {
                RaceDTO r = mapper.readValue(request.getInputStream(), RaceDTO.class);
                
                r.id = (long) Integer.parseInt(RestHelpers.getFirst(path));
                
                service.update(r);
            } catch (IllegalArgumentException | NullPointerException e) {
                RestHelpers.printErrorMessage(response);
            }
        } else {
            RestHelpers.printErrorMessage(response);
        }
        
    }
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
        super.init(config);
    }
}
